package com.example.hello_world.fragments2;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.hello_world.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link oneFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class oneFragment extends Fragment {
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "热门", "iOS", "Android"
            , "前端", "后端", "设计", "工具资源"
    };
    private inAdapter inAdapter;
    private ViewPager viewPager;

    private TabLayout intabLayout;
    public oneFragment() {
        // Required empty public constructor
    }


    public static oneFragment newInstance() {
        oneFragment fragment = new oneFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_one, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager=getActivity().findViewById(R.id.inviewpager);
        intabLayout=getActivity().findViewById(R.id.intabLayout);
        for(String s:mTitles){
            mFragments.add(inFragment.newInstance(s));
            intabLayout.addTab(intabLayout.newTab());
        }
        inAdapter=new inAdapter(getFragmentManager(),mTitles,mFragments);
        viewPager.setAdapter(inAdapter);
        intabLayout.setupWithViewPager(viewPager);
    }
}